using System;
using System.Collections.Specialized;
using Activa.CoolStorage;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core.Providers;

namespace CodeStory.Core
{
	public sealed class PortletTemplateCache : IRefreshable
	{
		private static PortletTemplateCache bag;

		public static PortletTemplateCache Instance {
			get {
				if (bag == null) {
					bag = new PortletTemplateCache();
				}
				return bag;
			}
		}

		private HybridDictionary dict;

		private PortletTemplateCache() {
			dict = new HybridDictionary(8, false);
			Initialize();
		}

		private void Initialize() {
			CSList<PortletTemplateObject> list = PortletTemplateObject.List();
			foreach (PortletTemplateObject template in list) {
				dict[template.CodeName] = template;
			}
			if (!dict.Contains("default")) {
				PortletTemplateObject t = PortletTemplateObject.New();
				t.CodeName = "default";
				t.FriendlyName = "Default";
				t.TemplateFilePath = CodeStoryConfig.DefaultPortletTemplate;
				t.TemplateID = Guid.Empty;
				dict["default"] = t;
				t.Save();
			}
		}

		public PortletTemplateObject this[string key] {
			get {
				if (dict.Contains(key)) {
					return (PortletTemplateObject)dict[key];
				}
				return (PortletTemplateObject)dict["default"];
			}
		}

		#region IRefreshable Members

		public void Refresh() {
			bag = null;
		}

		#endregion
	}
}